define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'equipment/index' + location.search,
                    add_url: 'equipment/add',
                    edit_url: 'equipment/edit',
                    del_url: 'equipment/del',
                    multi_url: 'equipment/multi',
                    import_url: 'equipment/import',
                    table: 'equipment',
                }
            });

            var table = $("#table");
            //当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                //这里我们手动设置底部的值
                if(data.key_admin){
                    table.bootstrapTable('hideColumn','protect_buttons');
                    table.bootstrapTable('hideColumn','warranty_buttons');
                }
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'marking', title: __('Marking'), operate: 'LIKE'},
                        {field: 'serial', title: __('Serial'), operate: 'LIKE'},
                        //{field: 'enclosure_data', title: __('Enclosure_data'), searchList: {"0":__('Enclosure_data 0'),"1":__('Enclosure_data 1')}, formatter: Table.api.formatter.normal},
                        {field: 'enclosure', title: __('Enclosure'), operate: 'LIKE'},
                        {field: 'software_version', title: __('Software_version'), operate: 'LIKE'},
                        {field: 'next', title: __('Next_text'), operate: false},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'warranty_buttons', title: __('报修情况'), table: table,
                            buttons: [
                                {
                                    name: 'warranty',
                                    title: __('报修情况'),
                                    classname: 'btn btn-xs btn-success btn-addtabs',
                                    text: '查看',
                                    //icon: 'fa fa-folder-o',
                                    url: function(row,column){
                                        var url='work/warranty?equipment_id='+row.id;
                                        return url;
                                    },
                                },
                            ],
                            formatter: Table.api.formatter.buttons, operate: false
                        },
                        {field: 'protect_buttons', title: __('维保情况'), table: table,
                            buttons: [
                                {
                                    name: 'protect',
                                    title: __('维保情况'),
                                    classname: 'btn btn-xs btn-success btn-addtabs',
                                    text: '查看',
                                    //icon: 'fa fa-folder-o',
                                    url: function(row,column){
                                        var url='work/protect?equipment_id='+row.id;
                                        return url;
                                    },
                                },
                            ],
                            formatter: Table.api.formatter.buttons, operate: false
                        },
                        {field: 'department.name', title: __('Department.name'), operate: 'LIKE'},
                        /*{field: 'a.id', title: __('A.id'), operate: 'false'},*/
                        {field: 'a.name', title: __('A.name'), operate: 'LIKE'},
                        {field: 'remark', title: __('Remark'), operate: false},
                        {field: 'buttons', title: __('下载'), table: table, events: Table.api.events.buttons,
                            formatter: function (row,index) {
                                return " <a className='download-btn' style='font-size: x-large;' href='"+index.image+"' download='download.jpg'><i class='fa fa-cloud-download'></i></a>";
                            }, operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $(document).on("fa.event.appendfieldlist", '.btn-append', function(e,obj){
                //通用的表单组件事件绑定和组件渲染
                Form.events.datetimepicker(obj);
            });
            $("#c-department_id").data("params", function(){
                const hospital_id = $("#c-hospital_id").val();
                const department_id = $("#c-department_id").val();
                return {hospital_id: hospital_id,department_id: department_id};
            });
            $("#c-equipment_id").data("params", function(){
                const department_id = $("#c-department_id").val();
                return {department_id: department_id};
            });
            Controller.api.bindevent();
        },
        edit: function () {
            $(document).on("fa.event.appendfieldlist", '.btn-append', function(e,obj){
                //通用的表单组件事件绑定和组件渲染
                Form.events.datetimepicker(obj);
            });
            $("#c-department_id").data("params", function(){
                const hospital_id = $("#c-hospital_id").val();
                const department_id = $("#c-department_id").val();
                return {hospital_id: hospital_id,department_id: department_id};
            });
            $("#c-equipment_id").data("params", function(){
                const department_id = $("#c-department_id").val();
                return {department_id: department_id};
            });
            Controller.api.bindevent();
        },
        api: {

            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
